import type { components } from '@/genapi/wh';
import { whManagerSystemSystemUserListPageUsingPost } from '@/genapi/wh';
import { usePage } from '@/hooks/use-page';
import { useCompanyStore } from '@/stores/company';

export const [createState, injectState] = createInjectionState(() => {
  const companyStore = useCompanyStore();

  const filter = ref<components['schemas']['SystemUserQueryDTO']>();

  const page = usePage({
    initPageSize: 15,
    fetch: async ({ pageSize, currentPage }) => {
      const { data } = await whManagerSystemSystemUserListPageUsingPost({
        data: {
          current: currentPage,
          size: pageSize,
          query: {
            ...filter.value,
            relatedId: companyStore.companyInfo?.id,
          },
        },
      });

      return {
        records: data?.records ?? [],
        total: data?.total ?? 0,
        pageSize,
      };
    },
  });

  watchDeep(filter, () => page.execute({ currentPage: 0 }));

  // 当前选中的列表项
  const selectedList = shallowRef<components['schemas']['SystemUserDTODuiXiang'][]>([]);

  return {
    page,
    filter,
    selectedList,
  };
});
